﻿using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    /// <summary>
    /// 地域与违法行为配置DAL
    /// </summary>
    public class Xzzf_DisWfRelDAL
    {
        /// <summary>
        /// 插入地域与违法行为表
        /// </summary>
        /// <param name="disWfRels">地域与违法行为models</param>
        /// <returns>返回执行是否成功 True成功 false失败</returns>
        public bool InsertDisWfRel(List<Xzzf_DisWfRel> disWfRels)
        {
            try
            {
                return SqlManage.sqlServerJudgeDocSystem.Insert<Xzzf_DisWfRel>(disWfRels).ExecuteAffrows() > 0 ? true : false;
            }
            catch (Exception ex)
            {
                throw new Exception("插入地域与违法行为表出错：" + ex.Message);
            }
        }

        /// <summary>
        /// 根据违法行为节点ccid获取配置地域信息
        /// </summary>
        /// <param name="ccId">违法行为节点ccid</param>
        /// <returns>返回以,拼接的地域信息</returns>
        public string GetSystrictByDisWfRel(string ccId)
        {
            try
            {
                List<string> sysDistrictNames = SqlManage.sqlServerJudgeDocSystem.Select<Xzzf_DisWfRel, SYS_District>().InnerJoin((a, b) => a.CC_District == b.DT_ID).Where((a, b) => a.WF_ID == ccId).ToList((a, b) => b.DT_Name);
                var systrictNames = string.Join(",", sysDistrictNames);
                return systrictNames;
            }
            catch (Exception ex)
            {
                throw new Exception("根据违法行为节点ccid获取配置地域信息出错："+ex.Message);
            }
        }

        /// <summary>
        /// 根据违法行为ccid删除地域与违法行为表关系数据
        /// </summary>
        /// <param name="ccId">违法行为ccid</param>
        /// <returns>返回执行是否成功 True成功 false失败</returns>
        public bool DelDisWfRelByWfid(string ccId)
        {
            try
            {
                return SqlManage.sqlServerJudgeDocSystem.Delete<Xzzf_DisWfRel>().Where(x => x.WF_ID == ccId).ExecuteAffrows() > 0 ? true : false;
            }
            catch (Exception ex)
            {
                throw new Exception("根据违法行为ccid删除地域与违法行为表关系数据出错："+ex.Message);
            }
        }

        /// <summary>
        /// 根据配置地域信息获取违法行为节点ccid
        /// </summary>
        /// <param name="ccDistricts">界面条件选择的地域</param>
        /// <returns>返回违法行为ccid集合</returns>
        public List<string> GetWfIdsBySystrict(List<string> ccDistricts)
        {
            try
            {
                if (ccDistricts.Contains("all"))
                {
                    return SqlManage.sqlServerJudgeDocSystem.Select<Xzzf_DisWfRel>().ToList(x => x.WF_ID).Distinct().ToList();
                }
                else
                {
                    return SqlManage.sqlServerJudgeDocSystem.Select<Xzzf_DisWfRel>().Where(x => ccDistricts.Contains(x.CC_District)).ToList(x => x.WF_ID).Distinct().ToList();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("根据配置地域信息获取违法行为节点ccid信息出错：" + ex.Message);
            }
        }

        /// <summary>
        /// 获取地域与违法行为配置信息
        /// </summary>
        /// <param name="ccId">违法行为ccid</param>
        /// <returns>返回地域与违法行为查询Model</returns>
        public List<Xzzf_DisWfRelCXModel> GetDisWfRelData(string ccId)
        {
            try
            {
                List<Xzzf_DisWfRelCXModel> models= SqlManage.sqlServerJudgeDocSystem.Select<Xzzf_DisWfRel, SYS_CaseCause, SYS_District>().InnerJoin((a, b, c) => a.WF_ID == b.CC_ID).InnerJoin((a, b, c) => a.CC_District == c.DT_ID).Where((a, b, c) => b.CC_ParentID == ccId).ToList((a, b, c) => new Xzzf_DisWfRelCXModel { RC_ID = a.RC_ID, CC_District = a.CC_District, DistrictName = c.DT_Name, WF_ID = a.WF_ID, CC_Name = b.CC_Name });
                return models;
            }
            catch (Exception ex)
            {
                throw new Exception("获取地域与违法行为配置信息出错："+ex.Message);
            }
        }
    }
}
